# @author:Wei Junjie
# @time:2024/5/25 21:40
# @file temp.py
# --*--coding: utf-8 --*--
"""

配置LOG

"""
import os
import time
import logging
import colorlog



class Mylogger:
    def __init__(self,):
        """
        :param level:[info,debug,warn,error,critical]
        """
        # 实例属性
        self.__log_obj=logging.getLogger(os.path.basename(__file__))
        self.__log_obj.setLevel(Mylogger.SET_LOG_LEVEL['debug'])
        # self.file_obj()
        self.console_obj()

    # 类属性
    BASE_DIR = os.getcwd()
    FORMAT = '[%(asctime)s] [%(levelname)s] [%(filename)s] :%(message)s'
    CONSOLE_FORMAT = '%(log_color)s[%(asctime)s] [%(levelname)s] [%(filename)s] :[%(message)s]'
    SET_LOG_LEVEL = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR
    }
    DEFAULT_TIME = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
    LOG_COLORS = {
        'DEBUG': 'cyan'
        , 'INFO': 'green'
        , 'WARNING': 'yellow'
        , 'ERROR': 'red'
        , 'CRITICAL': 'red'
    }


    # 创建文件对象
    # def file_obj(self):
    #     fh = logging.FileHandler(os.path.join(self.BASE_DIR,'{}.log'.format(Mylogger.DEFAULT_TIME)),'a','utf-8')
    #     fh.setLevel(Mylogger.SET_LOG_LEVEL['debug'])
    #     fh.setFormatter(logging.Formatter(Mylogger.FORMAT))
    #     self.__log_obj.addHandler(fh)

    # 创建终端输出对象
    def console_obj(self):
        ch = logging.StreamHandler()
        ch.setLevel(Mylogger.SET_LOG_LEVEL['debug'])
        color_formatter = colorlog.ColoredFormatter(Mylogger.CONSOLE_FORMAT, log_colors=Mylogger.LOG_COLORS,datefmt=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
        ch.setFormatter(color_formatter)
        self.__log_obj.addHandler(ch)

    def info(self,message):
        self.__log_obj.info(message)

    def debug(self,message):
        self.__log_obj.debug(message)

    def warning(self,message):
        self.__log_obj.warning(message)

    def error(self, message):
        self.__log_obj.error(message)

    def critical(self, message):
        self.__log_obj.critical(message)

if __name__ == '__main__':
    log=Mylogger()
    log.debug('sda')